Program rets_turn_easy_borrow
      character*6 ticker(5000)
      character*8 cusp(5000)
      integer(kind=8):: nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
      dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
      dimension nep(5000),price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),ntype(1264),turn(5000)
!      open(unit=10,file='e:\FIS\crsp_util_sorted_fees_2005_2023.txt')
!      open(unit=99,file='e:\FIS\rets_high_low_vol_easy_borrow.txt')
      open(unit=10,file='e:\FIS\crsp_util_sorted_fees_2005_2023_rep_b.txt')
      open(unit=99,file='e:\FIS\rets_high_low_vol_easy_borrow_rep.txt')
      iend=0
      iskip=0
      i=0
1     i=i+1
2     read(10,1000,end=900)jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),jvalue(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i),nvalue(i)      
1000  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f9.6,1x,i9,1x,f9.6,1x,f6.2,2(1x,i13)) 
      if(retrn(i).lt.-1.) go to 2
      turn(i)=float(ivol(i))/(1000.*float(ishrout(i)))
      if(favg(i).gt.1.) iskip=1
      if(jast(i).eq.jast(1)) go to 1
5     icnt=i-1
      
      if(icnt.lt.10) go to 500
      if(iskip.eq.1) go to 500
      avgt=0.
      do 10 ii=1,icnt
      avgt=avgt+turn(ii)
10    continue   
      avgt=avgt/float(icnt)
      reth=0.
      retl=0.
      cnth=0.
      cntl=0.
      turnh=0.
      turnl=0.
      do 30 ii=1,icnt
          if(turn(ii).ge.avgt) go to 20
          cntl=cntl+1.
          retl=retl+retrn(ii)
          turnl=turnl+turn(ii)
          go to 30
20        cnth=cnth+1.
          reth=reth+retrn(ii)
          turnh=turnh+turn(ii)
30    continue
      retl=retl/cntl
      reth=reth/cnth
      turnl=turnl/cntl
      turnh=turnh/cnth
      write(99,9900)jast(1),avgt,cntl,turnl,retl,cnth,turnh,reth
      write(6,9900)jast(1),avgt,cntl,turnl,retl,cnth,turnh,reth
9900  format(1x,i8,1x,f10.6,2(1x,f5.0,2(1x,f9.6)))      
      
500   jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      turn(1)=turn(i)
      iskip=0
      if(favg(1).gt.1.) iskip = 1
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program rets_turn_easy_borrow